package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.input.BA_WellInputDto;
import com.b511.swpu.cementbackend.dto.input.WorkBaseInputDto;
import com.b511.swpu.cementbackend.dto.output.DataClassificationOutputDto;
import com.b511.swpu.cementbackend.entity.BA_Well;
import com.b511.swpu.cementbackend.entity.Data_Classification;
import com.b511.swpu.cementbackend.repository.BA_WellRepository;
import com.b511.swpu.cementbackend.repository.Data_ClassificationRepository;
import com.b511.swpu.cementbackend.repository.WorkBaseNewRepository;
import com.b511.swpu.cementbackend.repository.WorkBaseRepository;
import com.b511.swpu.cementbackend.service.MenuService;
import com.b511.swpu.cementbackend.service.WorkBaseService;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {
    private final Data_ClassificationRepository dataClassificationRepository;

    public MenuServiceImpl(Data_ClassificationRepository dataClassificationRepository) {
        this.dataClassificationRepository = dataClassificationRepository;
    }

    @Override
    public R getMenu() throws Exception {
        List<Data_Classification> dataClassificationList = dataClassificationRepository.findAllAndOrderByCode();
        // System.out.println(dataClassificationList);
        List<Integer> codeList = new ArrayList<>();
        Map<String, DataClassificationOutputDto> map = new TreeMap<>();
        List<DataClassificationOutputDto> resultList = new ArrayList<>();
        //转换成树结构
        for(Data_Classification dataClassification:dataClassificationList) {
            DataClassificationOutputDto dto = new DataClassificationOutputDto();
            dto.setCode(dataClassification.getCode());
            dto.setName(dataClassification.getName());
            if (dto.getCode().length()>2){//父节点没有menuName
                dto.setMenuName(dataClassification.getMenuName());
            }
            map.put(dto.getCode(),dto);
            if(codeList.indexOf(dataClassification.getCode().length())==-1){
                codeList.add(dataClassification.getCode().length());
            }
        }
        Collections.sort(codeList);
        for(Map.Entry<String, DataClassificationOutputDto> entry:map.entrySet()) {
            String key = entry.getKey();
            if(key.length()==codeList.get(0)){
                resultList.add(entry.getValue());
            }else{
                DataClassificationOutputDto dataClassificationOutputDto = map.get(key.substring(0,codeList.get(codeList.indexOf(key.length())-1)));
                if (null == dataClassificationOutputDto)
                    continue;
                if (null==dataClassificationOutputDto.getChildren()) {
                    List<DataClassificationOutputDto> temp = new ArrayList<>();
                    temp.add(entry.getValue());
                    dataClassificationOutputDto.setChildren(temp);
                } else {
                    dataClassificationOutputDto.getChildren().add(entry.getValue());
                }
            }
        }
        return R.ok().putData(resultList);
    }
}
